package com.sinping.qh.api.app.version.v1.records;

import com.sinping.qh.api.app.base.BaseAppEndPoint;
import com.sinping.qh.api.support.Const;
import com.sinping.qh.api.support.ConstEum;
import com.sinping.qh.api.support.FrontPage;
import com.sinping.qh.api.support.ResDto;
import com.sinping.qh.dto.Insert;
import com.sinping.qh.dto.Update;
import com.sinping.qh.dto.app.records.CheckPlanByAppDTO;
import com.sinping.qh.dto.common.CommonDTO;
import com.sinping.qh.service.app.auth.ISessionMongoService;
import com.sinping.qh.service.app.records.ICheckPlanService;
import com.sinping.qh.utils.mapper.JsonMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author zhangjin
 * @create 2018/5/29
 */
@Api(value = "app检查计划",description = "app检查计划API")
@RestController
@RequestMapping(value = Const.APP_URI_V1+"/checkplan",name="检查计划")
public class CheckPlanEndPointV1 extends BaseAppEndPoint {


    @Autowired
    ICheckPlanService checkPlanService;

    @Autowired
    @SuppressWarnings("SpringJavaAutowiringInspection")
    ISessionMongoService iSessionMongoService;




    @ApiOperation(httpMethod = "GET", value = "检查计划列表")
    @RequestMapping(name = "检查计划列表", method = RequestMethod.GET)
    public ResDto list(@ApiParam(name="msg",value="{\"page_size\":10, \"page_current\":1, \"order_column\":\"gmt_created\", \"order_by\":\"desc\", \"condition\":{}}")
                           @RequestParam(required = true) String msg)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        FrontPage<CheckPlanByAppDTO> frontPage = JsonMapper.INSTANCE.fromJson(msg, FrontPage.class);

        //查询时检查查询条件格式
        checkNull(frontPage,ConstEum.PARAM_ERROR);

        CommonDTO commonDTO =new CommonDTO();
        commonDTO.setUserSessionDTO(getUserSessionDto());
        //查询数据
        frontPage =checkPlanService.findPage(frontPage,commonDTO);

        checkNull(frontPage, ConstEum.FAIL);

        resDto.setDatas(JsonMapper.defaultMapper().toJson(frontPage));
        resDto.setMsg(ConstEum.SUCCESS.getMessage());
        return resDto;
    }

    /**
     * {\"unit_id\":\"123\",\"unit_name\":\"testunit\",\"building_id\":\"testbuild\"\n" +
     " ,\"building_name\":\"textname\",\"union_unit_users\":\"textuser\",\n" +
     "\"check_users_id\":[\"5b0cb9a1aee9d1fa3bb40f61\",\"5b0cb9e4aee9d1fa3bb40fa2\"],\"create_user_id\":\"textid\",\n" +
     "\"update_user_id\":\"textudpateid\",\"create_user_name\":\"textname\"}
     * @param msg
     * @return
     */
    @ApiOperation(httpMethod = "POST", value = "验证是否有检查中和待审核检查记录")
    @RequestMapping(value="prevalid",name = "验证是否有检查中和待审核检查记录", method = RequestMethod.POST)
    public ResDto addPreValidPlan(@ApiParam(name="msg",value="")
                          @RequestParam(required = true) String msg)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        CheckPlanByAppDTO checkPlanByAppDTO=JsonMapper.defaultMapper().fromJson(msg, CheckPlanByAppDTO.class);
        checkNull(checkPlanByAppDTO,ConstEum.PARAM_ERROR);

        checkValidator(checkPlanByAppDTO,Insert.class,true);

        CommonDTO commonDTO =new CommonDTO();
        commonDTO.setUserSessionDTO(getUserSessionDto());

        String checkPlanId=checkPlanService.preValidCheck(checkPlanByAppDTO,commonDTO);
        if(StringUtils.isNotBlank(checkPlanId)) {
            resDto.setDatas("{\"check_plan_id\":\""+checkPlanId+"\"}");
            resDto.setMsg("存在未完成记录");
        }else {
            resDto.setDatas("{\"check_plan_id\":\"\"}");
            resDto.setMsg("不存在未完成记录");
        }
        return resDto;
    }

    /**
     * {\"unit_id\":\"123\",\"unit_name\":\"testunit\",\"building_id\":\"testbuild\"\n" +
     " ,\"building_name\":\"textname\",\"union_unit_users\":\"textuser\",\n" +
     "\"check_users_id\":[\"5b0cb9a1aee9d1fa3bb40f61\",\"5b0cb9e4aee9d1fa3bb40fa2\"],\"create_user_id\":\"textid\",\n" +
     "\"update_user_id\":\"textudpateid\",\"create_user_name\":\"textname\"}
     * @param msg
     * @return
     */
    @ApiOperation(httpMethod = "POST", value = "添加检查计划")
    @RequestMapping(name = "添加检查计划", method = RequestMethod.POST)
    public ResDto addPlan(@ApiParam(name="msg",value="")
                              @RequestParam(required = true) String msg)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        CheckPlanByAppDTO checkPlanByAppDTO=JsonMapper.defaultMapper().fromJson(msg, CheckPlanByAppDTO.class);
        checkNull(checkPlanByAppDTO,ConstEum.PARAM_ERROR);

        checkValidator(checkPlanByAppDTO,Insert.class,true);

        CommonDTO commonDTO =new CommonDTO();
        commonDTO.setUserSessionDTO(getUserSessionDto());

        checkPlanByAppDTO=checkPlanService.save(checkPlanByAppDTO,commonDTO);
        checkNull(checkPlanByAppDTO,ConstEum.FAIL);
        resDto.setDatas(JsonMapper.defaultMapper().toJson(checkPlanByAppDTO));
        resDto.setMsg(ConstEum.SUCCESS.getMessage());
        return resDto;
    }

    @ApiOperation(httpMethod = "PUT", value = "修改检查计划")
    @RequestMapping(name = "修改检查计划", method = RequestMethod.PUT)
    public ResDto updatePlan(@ApiParam(name="msg",value="{\"id\":\"5b14fc8c6a58d4226c20a701\",\"check_users_id\":[\"textuser\"],\"union_unit_users\":\"zhng\"}")
                          @RequestParam(required = true) String msg)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        CheckPlanByAppDTO checkPlanByAppDTO=JsonMapper.INSTANCE.fromJson(msg, CheckPlanByAppDTO.class);
        checkNull(checkPlanByAppDTO,ConstEum.PARAM_ERROR);

        checkValidator(checkPlanByAppDTO,Update.class,true);

        CommonDTO commonDTO =new CommonDTO();
        commonDTO.setUserSessionDTO(getUserSessionDto());
        checkPlanByAppDTO=checkPlanService.update(checkPlanByAppDTO,commonDTO);

        String data = JsonMapper.defaultMapper().toJson(checkPlanByAppDTO);
        resDto.setDatas(data);
        resDto.setMsg(ConstEum.SUCCESS.getMessage());
        return resDto;
    }
    @ApiOperation(httpMethod = "DELETE", value = "删除检查计划")
    @RequestMapping(value = "/{id}",name = "删除检查计划", method = RequestMethod.DELETE)
    public ResDto deletePlan(@PathVariable String id)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        checkPlanService.delete(id);
        resDto.setMsg(ConstEum.SUCCESS.getMessage());
        return resDto;
    }
    @ApiParam(name = "id")
    @ApiOperation(httpMethod = "GET", value = "检查计划明细")
    @RequestMapping(value = "/{id}",name = "检查计划明细", method = RequestMethod.GET)
    public ResDto planDetail(@PathVariable String id)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        CheckPlanByAppDTO checkPlanByAppDTO=  checkPlanService.findOne(id);

        String data=JsonMapper.defaultMapper().toJson(checkPlanByAppDTO);
        resDto.setDatas(data);
        resDto.setMsg(ConstEum.SUCCESS.getMessage());

        return resDto;
    }


    @ApiOperation(httpMethod = "GET", value = "离线检查计划(离线：检查中,不分页)")
    @RequestMapping(value = "/listoffline/{planid}",name = "离线检查计划(离线：检查中,不分页)", method = RequestMethod.GET)
    public ResDto listoffline(@PathVariable String planid)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);
        checkNull(planid,ConstEum.PARAM_ERROR);

        CommonDTO commonDTO =new CommonDTO();
        commonDTO.setUserSessionDTO(getUserSessionDto());
        //查询数据
        CheckPlanByAppDTO planOffLine = checkPlanService.findPlanOffLine(planid,commonDTO);

        checkNull(planOffLine, ConstEum.FAIL);

        resDto.setDatas(JsonMapper.defaultMapper().toJson(planOffLine));
        resDto.setMsg(ConstEum.SUCCESS.getMessage());
        return resDto;
    }


}
